﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using NRCP.BusinessLogicLayer;
using NRCP.BusinessEntities;
using NRCP.Helpers;
using Imgalib.Utility;

namespace NRCP
{
    public partial class Zonal_Plan : NationalPlanBase
    {
        List<NationalPlanDetailsYearly> yearlyPlans = null;
        List<NationalPlanFish> fishes = null;

        private List<Int32> SampleInput
        {
            get
            {
                return Enumerable.Repeat(0, 12).ToList<Int32>();

            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.PresentPageStep = NationalPlanSteps.MonthlyPlan;
                this.CheckSteps();
                LoadInputGrid();                
            }
        }


        private void LoadInputGrid()
        {                    
            NationalPlanDetailsYearlyBLL yearlyBll = new NationalPlanDetailsYearlyBLL();
            yearlyPlans = yearlyBll.GetNationalPlanDetailsYearliesByNationalPlanId(SiteUtility.IntQueryParam);
            
            NationalPlanFishBLL fishesbll = new NationalPlanFishBLL();
            fishes = fishesbll.GetNationalPlanFishByNationalPlanId(SiteUtility.IntQueryParam);

            NationalPlanDivisionBLL divisionBll = new NationalPlanDivisionBLL();
            repYearlyPlan.DataSource = divisionBll.GetNationalPlanDivisionsByNationalPlanId(SiteUtility.IntQueryParam); ;
            repYearlyPlan.DataBind();

            if (yearlyPlans.Count > 0)
            {
                if (yearlyPlans.Count(obj => obj.Status == PlanStatus.Pending.ToString()) <= 0)
                {
                    btnSaveAll.Visible = true;
                }
            }
        }

        protected void repYearlyPlan_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater repBreakdownByFish = e.Item.FindControl("repBreakdownByFish") as Repeater;
                if (repBreakdownByFish != null)
                {
                    repBreakdownByFish.DataSource = fishes;
                    repBreakdownByFish.DataBind();
                }
            }

        }

        protected void repBreakdownByFish_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Panel pnlPlanDetails = e.Item.FindControl("pnlPlanDetails") as Panel;
                Panel pnlEmpty = e.Item.FindControl("pnlEmpty") as Panel;
                Panel pnlOptions = e.Item.FindControl("pnlOptions") as Panel;

                Repeater repBreakdownByGroups = e.Item.FindControl("repBreakdownByGroups") as Repeater;
                HiddenField hidDivisionName = e.Item.FindControl("hidDivisionName") as HiddenField;
                Int32 divisionId=Convert.ToInt32(hidDivisionName.Value.Trim());
                if (pnlOptions!=null && pnlEmpty != null && pnlPlanDetails != null && repBreakdownByGroups != null && hidDivisionName != null && yearlyPlans != null)
                {
                    NationalPlanFish planFish = e.Item.DataItem as NationalPlanFish;
                    List<NationalPlanDetailsYearly> brkDownyearlyPlans = yearlyPlans.Where(obj => obj.FishId == planFish.Id && obj.DivisionId == divisionId).ToList();
                    if (brkDownyearlyPlans.Count > 0)
                    {
                        repBreakdownByGroups.DataSource = brkDownyearlyPlans;
                        repBreakdownByGroups.DataBind();
                    }
                    else
                    {
                        pnlPlanDetails.Visible = false;
                        pnlEmpty.Visible = true;
                        pnlOptions.Visible = false;
                    }
                }
            }
        }

        protected void btnSaveAll_Click(object sender, EventArgs e)
        {
            NationalPlanBLL bll = new NationalPlanBLL();
            NationalPlan plan=bll.GetNationalPlanById(SiteUtility.IntQueryParam);
            plan.Status = PlanStatus.Complete.ToString();
            bll.UpdateNationalPlan(plan);
            HttpContext.Current.Response.Redirect(SiteSettings.BaseUrl + "complete.aspx?params=" + Cryptography.Encrypt(SiteUtility.IntQueryParam.ToString()));
        }
    }
}